package com.waterfairy.note.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.waterfairy.note.R;
import com.waterfairy.note.utils.ShareUtils;
import com.waterfairy.note.utils.WebViewUtils;


public class WebViewActivity extends Activity {

    private WebView webView;
    private Handler handler;
    private AlertDialog netCheckDialog;
    private AlertDialog netErrorDialog;

    private final int CHECK_NUM = 36;
    private final int CHECK_TIME = CHECK_NUM * 5;

    //检查次数
    private int checkNum = CHECK_NUM;
    //检查倒计时
    private int checkTime = CHECK_TIME;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        getExtra();
        findView();
        initView();
    }


    private void findView() {
        webView = findViewById(R.id.webView);
    }

    private void getExtra() {
    }

    private void initView() {
        //返回按钮
        WebViewUtils.init(webView);
        webView.addJavascriptInterface(this, "MobileObject");
        loadUrl();
    }

    // MobileObject.invokeAndroid(macAddress,)
    @JavascriptInterface
    public void invokeAndroid(String commandCode, String commandContent) {
        WebViewUtils.invokeAndroid(this, webView, commandCode, commandContent);
    }

    @JavascriptInterface
    public void invokeAndroid(String commandCode) {
        WebViewUtils.invokeAndroid(this, webView, commandCode, null);
    }


    private void loadUrl() {
        webView.loadUrl(ShareUtils.getUrl(this));
    }

    @Override
    protected void onPause() {
        super.onPause();
        WebViewUtils.onPause(webView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        WebViewUtils.onResume(webView);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        WebViewUtils.releaseAllWebViewCallback();
    }
}